package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IDirectSeedClassDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.homeSite.DirectSeedClass;

public class DirectSeedClassDaoImpl extends HibernateDaoSupport implements IDirectSeedClassDao {

	public void add(DirectSeedClass directSeedClass) throws DataAccessException {
		this.getHibernateTemplate().save(directSeedClass);
	}

	public void del(DirectSeedClass directSeedClass) throws DataAccessException {
		this.getHibernateTemplate().delete(directSeedClass);
	}

	public void update(DirectSeedClass directSeedClass) throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(directSeedClass);
	}
	
	public DirectSeedClass loadById(Integer id) throws DataAccessException {
		Object obj = null;
		DirectSeedClass d = new DirectSeedClass();
		obj = this.getHibernateTemplate().get(DirectSeedClass.class, id);
		if(obj!=null){
			d = (DirectSeedClass)obj;
		}
		return d;
	}
	
	public DirectSeedClass loadByNameAndNameId(String lanmuname, Integer directSeedNameId) throws DataAccessException {
		String sql = " from DirectSeedClass m where 1 = 1 and m.lanmuname = ? and m.directSeedName.id = ?";
		List list = this.getHibernateTemplate().find(sql,new Object[]{lanmuname,directSeedNameId});
		DirectSeedClass directSeedClass = null ;
		if(list!=null && list.size()>0){
			directSeedClass = (DirectSeedClass)list.get(0);
		}
		return directSeedClass;
	}

	@SuppressWarnings("unchecked")
	public List<DirectSeedClass> loadByNameId(Integer directSeedNameId)
			throws DataAccessException {
		String sql = "from DirectSeedClass d where 1=1 and d.directSeedName.id = ? ";
		List<DirectSeedClass> list = this.getHibernateTemplate().find(sql,directSeedNameId);
		return list;
	}

	public PageResultSet loadPageList(Integer nameId, String lanmuname,
			Short iftj, Short shanchu, String beginDate, String endDate,
			Integer page, Integer pageSize) throws DataAccessException {
		PageResultSet pageResultSet = new PageResultSet();
	    PerfectPageResultSet pageResult = new PerfectPageResultSet();
	    // 获取Session
	    Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);

	    // 初始化SQL语句
	    StringBuffer stringBuffer = new StringBuffer(
	            " from DirectSeedClass as d  where 1=1 ");
	    
	    // 设置每页显示得记录数和开始页
	    pageResult.setPage(page, pageSize);

	    // 参数
	    List<Comparable> list = new ArrayList<Comparable>();
	    
	    // 直播名ID
	    if(nameId!=null && !nameId.equals(-1))
	    {
	        stringBuffer.append(" and d.directSeedName.id = ?");
	        list.add(nameId);
	    }
	    
	    // 栏目名
	    if(lanmuname!=null && !lanmuname.equals(""))
	    {
	    	lanmuname = "%"+lanmuname+"%";
	        stringBuffer.append(" and d.lanmuname like ?");
	        list.add(lanmuname);
	    }
	    
	    // 是否推荐 0 否 1 是
	    if(iftj!=null&&!iftj.equals((short)-1))
	    {
	    	stringBuffer.append(" and d.iftj = ?");
	        list.add(iftj);
	    }
	    
	    // 是否删除 0 否 1 是
	    if(shanchu!=null&&!shanchu.equals((short)-1))
	    {
	    	stringBuffer.append(" and d.shanchu = ?");
	        list.add(shanchu);
	    }
	    
	    // 时间段
	    if(null!=beginDate && !"".equals(beginDate) && null!=endDate && !"".equals(endDate)){
	    	stringBuffer.append(" and d.inputdate >= '"+beginDate.trim()+" 00:00:00' and d.inputdate <= '"+endDate.trim()+" 23:59:59'");
	    }
	    
	    stringBuffer.append(" order by id desc ");

	    // 把集合转换成数组
	    String[] strParams = Tool.toArray(list);
	    
	    pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
	    
	    return pageResultSet;
	}

	public DirectSeedClass getDirectSeedClassBylanmuname(String directSeedClass_scdt) throws DataAccessException {
		// TODO Auto-generated method stub
		
		DirectSeedClass directSeedClass=null;
		
		List list=this.getHibernateTemplate().find(" from DirectSeedClass as d where 1=1 and d.lanmuname = ? ", directSeedClass_scdt);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			directSeedClass=(DirectSeedClass) list.get(0);
		}
		
		return directSeedClass;
	}

	public List getDirectSeedClassByParentId(Integer id) throws DataAccessException {
		// TODO Auto-generated method stub
		String sql=" from DirectSeedClass as d where 1=1 and d.shanchu= 0 and d.directSeedName.id = ? ";
		List list=this.getHibernateTemplate().find(sql, id);
		return list;
	}
	
}





















